<?php
/**
 * Created by PhpStorm.
 * User: YAN
 * Date: 2017/7/21
 * Time: 13:21
 */

namespace App\Http\Controllers\API;


use App\Http\Controllers\Controller;
use App\Models\EnglishResults;
use App\Models\ExamResults;
use App\Models\Student;
use App\Models\StudentLog;
use Carbon\Carbon;
use Illuminate\Http\Request;

class UserExamController extends Controller
{
    public function index(Request $request, $id)
    {
        $queryString = $request->get('query', '');
        $query = \DB::table('student_exams')->join('exams', function ($join) {
            $join->on('student_exams.e_id', '=', 'exams.id');
        })->where(['student_exams.s_id' => $id])
            ->orderBy('exams.begin')->orderBy('exams.id');
        if ($queryString) {
            $query->where('exams.c_id', $queryString);
        }
        $exams = $query->paginate(5);
        return $exams;
    }

    public function course($id)
    {
        $res = [];
        $res[0] = '全部课程';
        $courses = \DB::table('courses')->join('student_courses', function ($join) {
            $join->on('student_courses.c_id', '=', 'courses.id');
        })->where(['student_courses.s_id' => $id])
            ->get();
        if ($courses) {
            foreach ($courses as $items) {
                $res[$items->c_id] = $items->c_name;
            }
        }
        return $res;
    }

    public function exams(Request $request, $id)
    {
        $queryString = $request->get('query', '');
        $query = \DB::table('student_exams')->join('exams', function ($join) {
            $join->on('student_exams.e_id', '=', 'exams.id');
        })->join('students', function ($join) {
            $join->on('students.id', '=', 'student_exams.s_id');
        })->where(['student_exams.e_id' => $id])
            ->orderBy('exams.begin')->orderBy('exams.id');
        if ($queryString) {
            $query->where('students.u_name', 'like', '%' . $queryString . '%')
                ->orwhere('students.u_code', 'like', '%' . $queryString . '%');
        }
        $exams = $query->paginate(5);
        return $exams;
    }

    public function englishs(Request $request, $id)
    {
        $queryString = $request->get('query', '');
        $query = \DB::table('student_eps')->join('english_proficiencys', function ($join) {
            $join->on('student_eps.ep_id', '=', 'english_proficiencys.id');
        })->join('students', function ($join) {
            $join->on('students.id', '=', 'student_eps.s_id');
        })->where(['student_eps.ep_id' => $id]);
        if ($queryString) {
            $query->where('students.u_name', 'like', '%' . $queryString . '%')
                ->orwhere('students.u_code', 'like', '%' . $queryString . '%');
        }
        $exams = $query->paginate(5);
        return $exams;
    }

    public function wxIndex(Request $request)
    {
        $queryString = $request->get('course_id', '');
        $open_id = $request->get('open_id', '');
        $res = [];
        $student = Student::query()->where(['wx_key' => $open_id])->first();
        if (!$student) {
            return response(['success' => 'false', 'message' => '未找到该学生'], 406);
        }
        $query = \DB::table('student_exams')->join('exams', function ($join) {
            $join->on('student_exams.e_id', '=', 'exams.id');
        })->where(['student_exams.s_id' => $student->id])
            ->orderBy('exams.begin')->orderBy('exams.id');
        if ($queryString) {
            $query->where('exams.c_id', $queryString);
        }
        $exams = $query->get();
        if ($exams) {
            foreach ($exams as $items) {
                $item['name'] = $items->e_name;
                $item['code'] = $items->e_code;
                $item['begin'] = date('Y-m-d H:i:s', $items->begin);
                $item['end'] = date('Y-m-d H:i:s', $items->end);
                $item['place'] = $items->place;
                $item['orders'] = $items->orders;
                $item['code'] = $items->code;
                $res[] = $item;
            }
        }
        $log = new StudentLog();
        $log->student_id = $student->id;
        $log->logs = '通过微信查看了考试信息';
        if (!$log->save()) {
            throw new \Exception('记录添加失败');
        }
        return ['success' => 'true', 'data' => $res, 'message' => '考试信息'];
    }

    public function wxCourse(Request $request)
    {
        $open_id = $request->get('open_id', '');
        $student = Student::query()->where(['wx_key' => $open_id])->first();
        if (!$student) {
            return response(['success' => 'false', 'message' => '未找到该学生'], 406);
        }
        $res = [];
        $res[] = ['id' => 0, 'name' => '全部课程'];
        $query = \DB::table('courses')->join('student_courses', function ($join) {
            $join->on('student_courses.c_id', '=', 'courses.id');
        })->where(['student_courses.s_id' => $student->id]);
        $courses = $query->get();
        if ($courses) {
            foreach ($courses as $key => $items) {
                $item['name'] = $items->c_name;
                $item['id'] = $items->c_id;
                $res[] = $item;
            }
        }
        return ['success' => 'true', 'data' => $res, 'message' => '课程信息'];
    }

    public function code(Request $request)
    {
        $open_id = $request->get('open_id', '');
        $s_id = $request->get('year', '');
        $student = Student::query()->where(['wx_key' => $open_id])->first();
        if (!$student) {
            return response(['success' => 'false', 'message' => '未找到该学生'], 406);
        }
        $res = [];
        $query = ExamResults::with(['Semesters'])->where(['s_id' => $student->id]);
        if ($s_id) {
            $query->where(['semester_id' => $s_id]);
        }
        $exams = $query->orderBy('time')->get();
        $log = new StudentLog();
        $log->student_id = $student->id;
        $log->logs = '通过微信端查看了考试成绩';
        if (!$log->save()) {
            throw new \Exception('记录添加失败');
        }
        if ($exams) {
            foreach ($exams as $items) {
                $item['time'] = date('Y-m-d', $items->time);
                $item['e_number'] = $items->e_number;
                $item['semester_name'] = $items->semesters->s_name;
                $item['e_name'] = $items->e_name;
                $item['serial_number'] = $items->serial_number;
                $item['credit'] = $items->credit;
                $item['e_codes'] = $items->e_codes;
                $item['e_jd_codes'] = $items->e_jd_codes;
                $item['s_name'] = $student->u_name;
                $item['s_code'] = $student->u_code;
                $res[] = $item;

            }
        }
        return ['success' => 'true', 'data' => $res, 'message' => '考试成绩'];

    }
}